/**
 * 
 */
package com.simtek.simplayer.utils;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.concurrent.TimeUnit;

import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.os.Build;

/**
 * @author OLA
 */
@SuppressLint("SimpleDateFormat")
public class TimeUtils {

	/**
	 * full time format
	 * 
	 * @param time
	 * @return
	 */
	public static String formatTime(Long time) {
		SimpleDateFormat format = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss a");
		return format.format(new Date(time));
	}

	@TargetApi(Build.VERSION_CODES.GINGERBREAD)
	public static String getDuration(long duration) {
		long hour = 0;
		long min = 0;
		long sec = 0;
		if (OsDetectUtils.hasGingerbread()) {
			hour = TimeUnit.MILLISECONDS.toHours(duration);
			min = TimeUnit.MILLISECONDS.toMinutes(duration);
			sec = TimeUnit.MILLISECONDS.toSeconds(duration);
		} else {
			// TODO getDuration
		}

		StringBuilder builder = new StringBuilder();
		if (hour != 0)
			builder.append(hour).append("h");
		if (min != 0)
			builder.append(min + 1).append("m");
		if (sec != 0)
			builder.append(sec + 1).append("s");

		return builder.toString();
	}

	@TargetApi(Build.VERSION_CODES.GINGERBREAD)
	public static String getDuration(long duration, String timeSeperate) {
		return getDuration(duration, timeSeperate, false);
	}

	public static String getDuration(long duration, String timeSeperate,
			boolean displayCentiSeconds) {
		long seconds = duration / 1000; // round down to compute seconds
		long minutes = seconds / 60;
		long hours = minutes / 60;
		long remainderMinutes = minutes - (hours * 60);
		long remainderSeconds = seconds - (minutes * 60);

		StringBuilder timeStringBuilder = new StringBuilder();
		if (timeSeperate == null) {
			timeSeperate = ":";
		}
		// Hours
		if (hours > 0) {
			if (hours < 10) {
				timeStringBuilder.append('0');
			}
			timeStringBuilder.append(hours);

			timeStringBuilder.append(timeSeperate);
		}

		// Minutes
		if (remainderMinutes < 10) {
			timeStringBuilder.append('0');
		}
		timeStringBuilder.append(remainderMinutes);
		timeStringBuilder.append(timeSeperate);

		// Seconds
		if (remainderSeconds < 10) {
			timeStringBuilder.append('0');
		}
		timeStringBuilder.append(remainderSeconds);

		// Centi seconds
		if (displayCentiSeconds) {
			timeStringBuilder.append('.');
			long remainderCentiSeconds = (duration - seconds * 1000) / 10;
			if (remainderCentiSeconds < 10) {
				timeStringBuilder.append('0');
			}
			timeStringBuilder.append(remainderCentiSeconds);
		}

		return timeStringBuilder.toString();
	}

	public static String getTimeAgo(long time, String[] timeLable) {
		// <item> mới đây</item>
		// <item> giây trước</item>
		// <item> phút trước</item>
		// <item> giờ trước</item>
		// <item> ngày trước</item>
		// <item> tuần trước</item>
		// <item> tháng trước</item>
		// <item> năm trước</item>

		long amount = time;
		amount = Math.abs(amount);

		/**
		 * seconds
		 */
		amount = amount / 1000;
		if (amount == 0) {
			return timeLable[0];
		}

		if (amount < 60) {
			return amount + " " + timeLable[1];
		}
		/**
		 * minutes
		 */
		amount = amount / 60;
		if (amount < 60) {
			return amount + " " + timeLable[2];
		}

		/**
		 * hours
		 */
		amount = amount / 60;
		if (amount < 24) {
			return amount + " " + timeLable[3];
		}
		/**
		 * days
		 */
		amount = amount / 24;
		if (amount < 30) {
			return amount + " " + timeLable[4];
		}
		// if (amount < 60) {
		// return 1 + SQText.txtMonthAgo;
		// }
		if (amount < 365) {
			return (amount / 30) + timeLable[6];
		}

		/**
		 * years
		 */
		amount = amount / 365;
		return amount + " " + timeLable[7];
	}

	public static String gethhmmYYYYMMDD(long time) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTimeInMillis(time);
		int year = calendar.get(Calendar.YEAR);
		int month = calendar.get(Calendar.MONTH) + 1;
		int date = calendar.get(Calendar.DATE);
		int hour = calendar.get(Calendar.HOUR_OF_DAY);
		int minute = calendar.get(Calendar.MINUTE);
		String s = "";
		if (hour < 10) {
			s += "0" + hour;
		} else {
			s += "" + hour;
		}
		if (minute < 10) {
			s += ":0" + minute;
		} else {
			s += ":" + minute;
		}

		if (date < 10) {
			s += "  0" + date;
		} else {
			s += "  " + date;
		}

		if (month < 10) {
			s += "/0" + month;
		} else {
			s += "/" + month;
		}
		s += "/" + year;

		return s;
	}

	public static String gethhmmYYYYMMDD(long time, String hourIndicator,
			String hourAndDateIndicator, String yearIndicator,
			String monthIndicator) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTimeInMillis(time);
		int year = calendar.get(Calendar.YEAR);
		int month = calendar.get(Calendar.MONTH) + 1;
		int date = calendar.get(Calendar.DATE);
		int hour = calendar.get(Calendar.HOUR_OF_DAY);
		int minute = calendar.get(Calendar.MINUTE);
		String s = "";
		if (hour < 10) {
			s += "0" + hour + hourIndicator;
		} else {
			s += hour + hourIndicator;
		}
		if (minute < 10) {
			s += minute + hourAndDateIndicator;
		} else {
			s += minute + hourAndDateIndicator;
		}
		s += (year + yearIndicator);
		if (month < 10) {
			s += "0" + month + monthIndicator;
		} else {
			s += month + monthIndicator;
		}
		if (date < 10) {
			s += "0" + date;
		} else {
			s += date;
		}

		return s;
	}

	public static String getTimeAgo(long time, String[] dateLabel,
			String[] timeLabel) {
		Calendar calendar = Calendar.getInstance();
		long currentTimeMillis = System.currentTimeMillis();
		calendar.setTimeInMillis(currentTimeMillis);
		int todayDayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);
		int todayDate = calendar.get(Calendar.DATE);
		int todayYear = calendar.get(Calendar.YEAR);
		int todayWeek = calendar.get(Calendar.WEEK_OF_YEAR);

		calendar.setTimeInMillis(currentTimeMillis - time);
		int hour = calendar.get(Calendar.HOUR_OF_DAY);
		int minute = calendar.get(Calendar.MINUTE);
		int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);
		int date = calendar.get(Calendar.DATE);
		int month = (calendar.get(Calendar.MONTH) + 1);
		int year = calendar.get(Calendar.YEAR);
		int week = calendar.get(Calendar.WEEK_OF_YEAR);
		String result = dateLabel[0] + " " + ((hour < 10) ? "0" + hour : hour)
				+ ":" + ((minute < 10) ? "0" + minute : minute) + " ";
		if (year == todayYear) {
			if (week == todayWeek) {
				if (date == todayDate) {
					long amount = time;
					amount = Math.abs(amount);

					/**
					 * seconds
					 */
					amount = amount / 1000;
					if (amount == 0) {
						return timeLabel[0];
					}

					if (amount < 60) {
						return amount + " " + timeLabel[1];
					}
					/**
					 * minutes
					 */
					amount = amount / 60;
					if (amount < 60) {
						return amount + " " + timeLabel[2];
					}

					/**
					 * hours
					 */
					amount = amount / 60;
					if (amount < 7) {
						return amount + " " + timeLabel[3];
					}
					result += dateLabel[8];
				} else {
					if (todayDate - date > 2) {
						switch (dayOfWeek) {
						case Calendar.MONDAY:
							result += dateLabel[1];
							break;
						case Calendar.TUESDAY:
							result += dateLabel[2];
							break;
						case Calendar.WEDNESDAY:
							result += dateLabel[3];
							break;
						case Calendar.THURSDAY:
							result += dateLabel[4];
							break;
						case Calendar.FRIDAY:
							result += dateLabel[5];
							break;
						case Calendar.SATURDAY:
							result += dateLabel[6];
							break;
						case Calendar.SUNDAY:
							result += dateLabel[7];
							break;

						default:
							break;
						}
					} else if (todayDate - date > 1) {
						result += dateLabel[10];
					} else if (todayDate - date > 0) {
						result += dateLabel[9];
					} else {
						switch (dayOfWeek) {
						case Calendar.MONDAY:
							result += (dateLabel[1] + " "
									+ ((date < 10) ? "0" + date : date) + "/" + ((month < 10) ? "0"
									+ month
									: month));
							break;
						case Calendar.TUESDAY:
							result += (dateLabel[2] + " "
									+ ((date < 10) ? "0" + date : date) + "/" + ((month < 10) ? "0"
									+ month
									: month));
							break;
						case Calendar.WEDNESDAY:
							result += (dateLabel[3] + " "
									+ ((date < 10) ? "0" + date : date) + "/" + ((month < 10) ? "0"
									+ month
									: month));
							break;
						case Calendar.THURSDAY:
							result += (dateLabel[4] + " "
									+ ((date < 10) ? "0" + date : date) + "/" + ((month < 10) ? "0"
									+ month
									: month));
							break;
						case Calendar.FRIDAY:
							result += (dateLabel[5] + " "
									+ ((date < 10) ? "0" + date : date) + "/" + ((month < 10) ? "0"
									+ month
									: month));
							break;
						case Calendar.SATURDAY:
							result += (dateLabel[6] + " "
									+ ((date < 10) ? "0" + date : date) + "/" + ((month < 10) ? "0"
									+ month
									: month));
							break;
						case Calendar.SUNDAY:
							result += (dateLabel[7] + " "
									+ ((date < 10) ? "0" + date : date) + "/" + ((month < 10) ? "0"
									+ month
									: month));
							break;

						default:
							break;
						}
					}
				}
			} else if ((todayWeek - 1) == week) {
				if ((todayDayOfWeek + 7) - dayOfWeek == 1) {
					result += dateLabel[9];
				} else if ((todayDayOfWeek + 7) - dayOfWeek == 2) {
					result += dateLabel[10];
				} else {
					switch (dayOfWeek) {
					case Calendar.MONDAY:
						result += (dateLabel[1] + " "
								+ ((date < 10) ? "0" + date : date) + "/" + ((month < 10) ? "0"
								+ month
								: month));
						break;
					case Calendar.TUESDAY:
						result += (dateLabel[2] + " "
								+ ((date < 10) ? "0" + date : date) + "/" + ((month < 10) ? "0"
								+ month
								: month));
						break;
					case Calendar.WEDNESDAY:
						result += (dateLabel[3] + " "
								+ ((date < 10) ? "0" + date : date) + "/" + ((month < 10) ? "0"
								+ month
								: month));
						break;
					case Calendar.THURSDAY:
						result += (dateLabel[4] + " "
								+ ((date < 10) ? "0" + date : date) + "/" + ((month < 10) ? "0"
								+ month
								: month));
						break;
					case Calendar.FRIDAY:
						result += (dateLabel[5] + " "
								+ ((date < 10) ? "0" + date : date) + "/" + ((month < 10) ? "0"
								+ month
								: month));
						break;
					case Calendar.SATURDAY:
						result += (dateLabel[6] + " "
								+ ((date < 10) ? "0" + date : date) + "/" + ((month < 10) ? "0"
								+ month
								: month));
						break;
					case Calendar.SUNDAY:
						result += (dateLabel[7] + " "
								+ ((date < 10) ? "0" + date : date) + "/" + ((month < 10) ? "0"
								+ month
								: month));
						break;

					default:
						break;
					}
				}
			} else {
				switch (dayOfWeek) {
				case Calendar.MONDAY:
					result += (dateLabel[1] + " "
							+ ((date < 10) ? "0" + date : date) + "/" + ((month < 10) ? "0"
							+ month
							: month));
					break;
				case Calendar.TUESDAY:
					result += (dateLabel[2] + " "
							+ ((date < 10) ? "0" + date : date) + "/" + ((month < 10) ? "0"
							+ month
							: month));
					break;
				case Calendar.WEDNESDAY:
					result += (dateLabel[3] + " "
							+ ((date < 10) ? "0" + date : date) + "/" + ((month < 10) ? "0"
							+ month
							: month));
					break;
				case Calendar.THURSDAY:
					result += (dateLabel[4] + " "
							+ ((date < 10) ? "0" + date : date) + "/" + ((month < 10) ? "0"
							+ month
							: month));
					break;
				case Calendar.FRIDAY:
					result += (dateLabel[5] + " "
							+ ((date < 10) ? "0" + date : date) + "/" + ((month < 10) ? "0"
							+ month
							: month));
					break;
				case Calendar.SATURDAY:
					result += (dateLabel[6] + " "
							+ ((date < 10) ? "0" + date : date) + "/" + ((month < 10) ? "0"
							+ month
							: month));
					break;
				case Calendar.SUNDAY:
					result += (dateLabel[7] + " "
							+ ((date < 10) ? "0" + date : date) + "/" + ((month < 10) ? "0"
							+ month
							: month));
					break;

				default:
					break;
				}
			}
		} else {
			switch (dayOfWeek) {
			case Calendar.MONDAY:
				result += (dateLabel[1] + " "
						+ ((date < 10) ? "0" + date : date) + "/"
						+ ((month < 10) ? "0" + month : month) + "/" + year);
				break;
			case Calendar.TUESDAY:
				result += (dateLabel[2] + " "
						+ ((date < 10) ? "0" + date : date) + "/"
						+ ((month < 10) ? "0" + month : month) + "/" + year);
				break;
			case Calendar.WEDNESDAY:
				result += (dateLabel[3] + " "
						+ ((date < 10) ? "0" + date : date) + "/"
						+ ((month < 10) ? "0" + month : month) + "/" + year);
				break;
			case Calendar.THURSDAY:
				result += (dateLabel[4] + " "
						+ ((date < 10) ? "0" + date : date) + "/"
						+ ((month < 10) ? "0" + month : month) + "/" + year);
				break;
			case Calendar.FRIDAY:
				result += (dateLabel[5] + " "
						+ ((date < 10) ? "0" + date : date) + "/"
						+ ((month < 10) ? "0" + month : month) + "/" + year);
				break;
			case Calendar.SATURDAY:
				result += (dateLabel[6] + " "
						+ ((date < 10) ? "0" + date : date) + "/"
						+ ((month < 10) ? "0" + month : month) + "/" + year);
				break;
			case Calendar.SUNDAY:
				result += (dateLabel[7] + " "
						+ ((date < 10) ? "0" + date : date) + "/"
						+ ((month < 10) ? "0" + month : month) + "/" + year);
				break;

			default:
				break;
			}
		}
		return result;
	}
}
